package org.ziegler.log.analysis.core;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ProcessThreadPool {

    /**
     * 执行线程池
     */
    static ExecutorService executorService = Executors.newSingleThreadExecutor();

    public static void shutdown() {
        executorService.shutdown();
        try {
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void SetThreadPoolNum(int num) {
        if (num > 0 && num <= 100) {
            if (executorService != null) {
                executorService.shutdown();
                try {
                    executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                executorService = null;
            }

            executorService = Executors.newFixedThreadPool(num);
        } else {
            System.out.println("设置线程数量不在(0, 100]范围内。num = " + num);
        }
    }
}
